jQuery.noConflict();
String.prototype.startsWith = function(str){
    return (this.indexOf(str) === 0);
}
jQuery.fn.ytplaylist = function(options) {
  var options = jQuery.extend( {
    holderId: 'ytvideo',
	playerHeight: 220,
	playerWidth: 280,
	addThumbs: false,
	thumbSize: 'small',
	showInline: false,
	autoPlay: false,
	showRelated: false,
	allowFullScreen: false
  },options); 
  return this.each(function() {
      var $el = jQuery(this);		
		var autoPlay = "";
		var showRelated = "&rel=0";
		var fullScreen = "";
		if(options.autoPlay) autoPlay = "&autoplay=1"; 
		if(options.showRelated) showRelated = "&rel=1"; 
		if(options.allowFullScreen) fullScreen = "&fs=1";
		function playOld(id) {
		   var html  = '';	
		   html += '<object height="'+options.playerHeight+'" width="'+options.playerWidth+'">';
		   html += '<param name="movie" value="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"> </param>';
		   html += '<param name="wmode" value="transparent"> </param>';
		   if(options.allowFullScreen) { 
		   		html += '<param name="allowfullscreen" value="true"> </param>'; 
		   }
		   html += '<embed src="http://www.youtube.com/v/'+id+autoPlay+showRelated+fullScreen+'"';
		   if(options.allowFullScreen) { 
		   		html += ' allowfullscreen="true" '; 
		   	}
		   html += 'type="application/x-shockwave-flash" wmode="transparent"  height="'+options.playerHeight+'" width="'+options.playerWidth+'"></embed>';
		   html += '</object>';			
		   return html;
		};
		function playNew (id) {
		  var html = '';
		  html += '<iframe width="'+ options.playerWidth +'" height="'+ options.playerHeight +'"';
		  html += ' src="http://www.youtube.com/embed/'+ id +'" frameborder="0"';
		  hml += ' allowfullscreen></iframe>';
		}
		function youtubeid(url) {
			var ytid = url.match("[\\?&]v=([^&#]*)");
			ytid = ytid[1];
			return ytid;
		};
		$el.children('li').each(function() {
		    jQuery(this).find('a').each(function () {
		        var thisHref = jQuery(this).attr('href');
                if (thisHref.startsWith('http://www.youtube.com')) {
                    jQuery(this).addClass('yt-vid');
                    jQuery(this).data('yt-id', youtubeid(thisHref));
                }
                else if (thisHref.startsWith('http://youtu.be')) {
                    jQuery(this).addClass('yt-vid');
                    var id = thisHref.substr(thisHref.lastIndexOf("/") + 1);
                    jQuery(this).data('yt-id', id);
                }
                else {
                    jQuery(this).addClass('img-link');
                }
            });
		});
		$el.children("li").children("a.yt-vid").click(function() {
			if(options.showInline) {
			    jQuery("li.currentvideo").removeClass("currentvideo");
			    jQuery(this).parent("li").addClass("currentvideo").html(playOld(jQuery(this).data("yt-id")));
			}
			else {
			    jQuery("#" + options.holderId + "").html(playOld(jQuery(this).data("yt-id")));
			    jQuery(this).parent().parent("ul").find("li.currentvideo").removeClass("currentvideo");
			    jQuery(this).parent("li").addClass("currentvideo");
			}	
			return false;
		});
		$el.find("a.img-link").click(function() {
		    var $img = jQuery('<img/>');
		    $img.attr({
		        src: jQuery(this).attr('href')
		    })
		        .css({
		            display: 'none',
		            position: 'absolute',
		            left: '0px',
		            top: '50%'});
		    if(options.showInline) {
		        jQuery("li.currentvideo").removeClass("currentvideo");
		        jQuery(this).parent("li").addClass("currentvideo").html($img);
	        }
	        else {

	            jQuery("#" + options.holderId + "").html($img);
	            jQuery(this).closest("ul").find("li.currentvideo").removeClass("currentvideo");
	            jQuery(this).parent("li").addClass("currentvideo");
				
	        }
            setTimeout(function() {
                if ( $img.height() < $img.width() ) {
                    $img.width(options.playerWidth).css('margin-top',parseInt($img.height()/-2, 10)).css({
                        height: 'auto'
                    });
                }
                else {
                    $img.css({
                        height: options.playerHeight,
                        width: 'auto',
                        top: '0px',
                        position: 'relative'
                    });
                }
                $img.fadeIn();
            }, 100);
		    return false;
	    });
		if(options.addThumbs) {
			$el.children().each(function(i){
			    var $link = jQuery(this).find('a:first');
			    var replacedText = jQuery(this).text();
				if ($link.hasClass('yt-vid')) {				    
				    if(options.thumbSize == 'small') {
    					var thumbUrl = "http://img.youtube.com/vi/"+$link.data("yt-id")+"/2.jpg";
    				}
    				else {
    					var thumbUrl = "http://img.youtube.com/vi/"+$link.data("yt-id")+"/0.jpg";
    				}

    				var thumbHtml = "<img src='"+thumbUrl+"' alt='"+replacedText+"' />";
    				$link.empty().html(thumbHtml+replacedText).attr("title", replacedText);				    
				}
				else {
				    var $img = jQuery('<img/>').attr('src', $link.attr('href'));
				    $link.empty().html($img).attr("title", replacedText);
				}				
			});			
		}
		
		var firstVid = $el.children("li:first-child").addClass("currentvideo").children("a").click(); 
  }); 
};